<template>
  <el-dialog width="500px" v-bind="$attrs" @close="handleClose" @open="handleOpen">
    <el-form
      @submit.prevent.native
      label-position="top"
      :model="form"
      ref="categoryForm"
      :rules="rules"
    >
      <el-form-item :label="$t('appstore.application.mrchName') + ':'" prop="merchantId">
        <el-input
          maxlength="32"
          show-word-limit
          v-model="form.merchantId"
          @blur="() => (form.merchantId = form.merchantId.trim())"
          :placeholder="$t('page.tip_select')"
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('batch.model') + ':'" prop="modelCode">
        <el-select
          v-model="form.modelCode"
          clearable
          :placeholder="$t('batch.model')"
          style="width: 100%"
        >
          <el-option
            v-for="item in modelList"
            :key="item.modelCode"
            :value="item.modelCode"
            :label="item.modelName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="SN" prop="sn">
        <el-input
          type="textarea"
          maxlength="256"
          show-word-limit
          v-model="form.sn"
          @blur="() => (form.sn = form.sn.trim())"
          placeholder="SN"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button text @click="handleClose()">{{ $t('common.cancel') }}</el-button>
        <el-button type="primary" :loading="confirmLoading" @click="confirm()">{{
          $t('common.confirm')
        }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script>
import { getModelList, addTerminal } from '@/api/tms'
export default {
  data() {
    return {
      confirmLoading: false,
      form: {
        merchantId: '',
        modelCode: '',
        sn: ''
      },
      modelList: []
    }
  },
  mounted() {
    this.getModel()
  },
  methods: {
    getModel() {
      getModelList()
        .then((res) => {
          this.modelList = res
        })
        .catch((err) => {
          this.$confirmError(err)
        })
    },
    handleOpen() {
      this.confirmLoading = false
    },
    handleClose() {
      this.confirmLoading = false
      //   this.$refs["form"].resetFields();
      this.$emit('update:model-value', false)
    },
    confirm() {
      this.$confirm(this.$t('modules.pass.terminal_delete_tip'), this.$t('modules.pass.warning'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'info'
      })
        .then(() => {
          let data = {
            merchantId: this.form.merchantId,
            modelCode: this.form.modelCode,
            sns: this.form.sn
          }
          addTerminal(data)
            .then((res) => {
              this.$emit('update:model-value', false)
              this.$emit('refresh')
              this.$message({
                type: 'success',
                message: this.$t('modules.pass.delete_complete')
              })
            })
            .catch((err) => {
              this.$confirmError(err)
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: this.$t('modules.pass.add_canceled')
          })
        })
    }
  }
}
</script>

<style></style>
